 var vm = new Vue({
     el: '#app',
     data: {
         memberId: common.getParam('memberId') || '',
         member: {},
         // 当前选中的某一卷书
         currBook: {},
         // 是不是新约 
         isNew: true,
         // 当前显示的书卷列表
         currBookList: [],
         // 书籍列表
         bookList: [],
         // 章列表
         chapterList: [],
         // 要展示的章节列表
         currChapterList: [],
         // 该书已打卡的节
         cardList: []
     },

     created() {
         this.getBookList();
     },

     methods: {
         // 获取用户信息
         getMember() {
             common.get('/member/detail', {
                 memberId: this.memberId
             }).then(res => {
                 this.member = res.data.data;
                 this.currBook = this.bookList.find(item => item.name === this.member.book) || this.currBookList[0];
                 this.getChapterListByBookId(this.currBook.bookId);
                 this.getCardList(this.currBook.bookId);
             })
         },

         //  获取已打卡的数据
         getCardList(bookId) {
             var memberId = this.memberId;
             common.get('/card/list', {
                 bookId,
                 memberId
             }).then(res => {
                 this.cardList = res.data.data;
                 this.cardList.forEach(item => {
                     let chapter = this.currChapterList.find(c => c.chapterId === item.chapterId)
                     chapter.readed = true;
                     chapter.cardId = item.cardId;

                 })
             })
         },

         daka(item) {

             // 已打卡
             if (item.readed) {
                 item.readed = false;
                 var data = {
                     cardId: item.cardId
                 }
                 common.post('/card/del', data).then(res => {
                     item.readed = false;
                 });
             } else {
                 var data = {
                     ...item,
                     memberId: this.memberId
                 }
                 common.post('/card/add', data).then(res => {
                     item.readed = true;
                 });
             }



         },

         // 切换书籍
         changeBook(item) {
             this.currBook = item;
             this.getChapterListByBookId(item.bookId);
             // 修改用户最近读的书籍
             common.post('/member/edit', {
                 memberId: this.memberId,
                 book: item.name
             }).then(res => {
                 //  this.member.bookId = item.bookId;
             })
         },
         // 根据书名获取章节列表
         getChapterListByBookId(bookId) {
             common.get('/chapter/list', {
                 bookId: bookId + ''
             }).then(res => {
                 this.currChapterList = res.data.data.map(item => {
                     return {
                         ...item,
                         readed: false
                     }
                 });
             })
         },

         //  切换新旧约
         changeCurrBookList(flag) {
             this.isNew = !this.isNew;
             if (flag == 1) {
                 this.currBookList = this.bookList.filter(item => item.isNew);
             } else {
                 this.currBookList = this.bookList.filter(item => !item.isNew);
             }
             //  this.currBook = this.currBookList.find(item => item.name === this.member.book) || this.currBookList[0];
         },
         getBookList() {
             common.get('/book/all').then(res => {
                 var list = res.data.data;
                 this.currBookList = list.filter(item => item.isNew);
                 this.bookList = list;
                 this.getMember();
             })
         },

         //  getChapterList() { 
         //      common.get('/chapter/list').then(res => {
         //          var list = res.data.data;
         //          this.currBookList = list.filter(item => item.isNew);
         //          this.bookList = list;
         //      })
         //  },

         isdoing(item) {
             var chapterListInBook = this.chapterList.filter(
                 (c) => c.bookId === item.id
             );
             var readedList = chapterListInBook.filter((c) => c.readed);
             if (readedList.length > 0 && readedList.length < item.num) {
                 return true;
             } else {
                 return false;
             }
         },
     }
 })